<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/30
 * Time: 19:51
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Cards;
use Log;

class CardController extends Controller
{

    public function save(Request $request, Cards $cards)
    {
        $cards->type = $request->input('type');
        $cards->name = $request->input('name');
        $cards->logo = $request->input('logo');
        $cards->link = $request->input('link');
        $cards->describ = $request->input('describ');
        $cards->reasons = $request->input('reasons');
        $cards->sorts = $request->input('sorts');
        $cards->save();
        return $this->success();
    }

    public function update(Request $request)
    {
        $v = $request->all();
        //修改
        Cards::where('id', $v['id'])
            ->where('type', $v['type'])
            ->update([
                'name' => $v['name'],
                'logo' => $v['logo'],
                'link' => $v['link'],
                'describ' => $v['describ'],
                'reasons' => $v['reasons'],
                'sorts' => $v['sorts'],
            ]);

        return $this->success();
    }

    public function delete(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id <= 0) {
            return $this->error();
        }
        Cards::destroy($id);
        return $this->success();
    }


    public function lists(Request $request, Cards $cards)
    {
        $type = (int)$request->get('type', 0);
        $pageSize = $request->get('pageSize', 10);
        if ($type == 0) {
            return $this->error();
        }
        $list = $cards::where('type', $type)->orderBy('sorts', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function mobileLists(Request $request, Cards $cards)
    {
        $type = (int)$request->get('type', 0);
        $pageSize = $request->get('pageSize', 10);
        if ($type == 0) {
            return $this->error();
        }
        $list = $cards::where('type', $type)->orderBy('sorts', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function info(Request $request)
    {
        $id = (int)$request->get('id');
        $res = Cards::find($id);
        return $this->success($res);
    }

    public function mobileInfo(Request $request)
    {
        $id = (int)$request->get('id');
        $res = Cards::find($id);
        return $this->success($res);
    }


}